package edu.yctc.weblog.controller.rest.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.constant.URLConstant;
import edu.yctc.weblog.controller.rest.LinkRestController;
import edu.yctc.weblog.entity.LinkDO;
import edu.yctc.weblog.entity.PageDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.service.LinkService;
import edu.yctc.weblog.utils.PageQueryUtil;
import edu.yctc.weblog.utils.VOUtil;
import edu.yctc.weblog.vo.SaveLinkVO;
import edu.yctc.weblog.vo.UpdateLinkVO;

@RestController
@RequestMapping(URLConstant.S + URLConstant.LINK)
public class LinkRestControllerImpl implements LinkRestController {

    @Autowired
    private LinkService linkService;

    @Override
    @GetMapping(URLConstant.LINK_GET_LINK_LIST)
    public ResultDO<PageDO> getLinkList(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || params.get("limit") == null)
            return new ResultDO<PageDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        return linkService.getBlogLinkPage(new PageQueryUtil(params));
    }

    @Override
    @PostMapping(URLConstant.LINK_SAVE_LINK)
    public ResultDO<Void> save(@ModelAttribute SaveLinkVO saveLinkVO) {
        if (saveLinkVO.getLinkType() <= 0 || StringUtils.isBlank(saveLinkVO.getLinkName())
            || StringUtils.isBlank(saveLinkVO.getLinkUrl()) || StringUtils.isBlank(saveLinkVO.getLinkDescription())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        LinkDO linkDO = VOUtil.coverSaveLinkVO2LinkDO(saveLinkVO);
        return linkService.saveLink(linkDO);
    }

    @Override
    @GetMapping(URLConstant.LINK_INFO + URLConstant.S + "{id}")
    public ResultDO<LinkDO> info(@PathVariable("id") Long id) {
        if (id <= 0) {
            return new ResultDO<LinkDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return linkService.getLinkDOById(id);
    }

    @Override
    @PostMapping(URLConstant.LINK_UPDATE)
    public ResultDO<Void> update(@ModelAttribute UpdateLinkVO updateLinkVO) {
        if (updateLinkVO.getLinkId() <= 0 || updateLinkVO.getLinkType() <= 0
            || StringUtils.isBlank(updateLinkVO.getLinkName()) || StringUtils.isBlank(updateLinkVO.getLinkUrl())
            || StringUtils.isBlank(updateLinkVO.getLinkDescription())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        LinkDO linkDO = linkService.getLinkDOById(updateLinkVO.getLinkId()).getModule();
        if (linkDO == null) {
            return new ResultDO<>(false, ResultCode.DB_ERROR, ResultCode.MSG_DB_ERROR);
        }
        linkDO.setType(updateLinkVO.getLinkType());
        linkDO.setRank(updateLinkVO.getLinkRank());
        linkDO.setName(updateLinkVO.getLinkName());
        linkDO.setUrl(updateLinkVO.getLinkUrl());
        linkDO.setDescription(updateLinkVO.getLinkDescription());
        return linkService.updateLink(linkDO);
    }

    @Override
    @PostMapping(URLConstant.LINK_DELETE)
    public ResultDO<Void> delete(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return linkService.deleteBatch(ids);
    }

}
